package com.dis.resource.service.impl;

import com.dis.common.constant.MessageConstant;
import com.dis.common.exception.AlreadyExistException;
import com.dis.pojo.dto.RegionDTO;
import com.dis.pojo.entity.Region;
import com.dis.pojo.vo.RegionTreeVO;
import com.dis.resource.mapper.RegionMapper;
import com.dis.resource.service.RegionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class RegionServiceImpl implements RegionService {

    @Autowired
    private RegionMapper regionMapper;

    @Override
    public List<RegionTreeVO> getRegionTree() {
        // 查询所有区域
        List<Region> allRegions = regionMapper.selectAll();
        // 构建树形结构
        return buildTree(allRegions, null);
    }

    @Override
    public Region getById(Integer id) {
        return regionMapper.selectById(id);
    }

    @Override
    public void save(RegionDTO regionDTO) {
        Region region = new Region();
        BeanUtils.copyProperties(regionDTO, region);
        regionMapper.insert(region);
    }

    @Override
    public void update(RegionDTO regionDTO) {
        Region region = new Region();
        BeanUtils.copyProperties(regionDTO, region);
        regionMapper.update(region);
    }

    @Override
    public void deleteById(Integer id) {
        // 检查是否有子区域
        int childCount = regionMapper.countByParentId(id);
        if (childCount > 0) {
            throw new AlreadyExistException(MessageConstant.Already_Exist);
        }
        regionMapper.deleteById(id);
    }

    @Override
    public void changeStatus(Integer id, Integer status) {
        Region region = new Region();
        region.setRegionId(id);
        region.setIsActive(status);
        regionMapper.update(region);
        // regionMapper.updateChildStatus(id, status);
    }

    @Override
    public List<Region> getRegionsByParentId(Integer parentId) {
        return regionMapper.selectByParentId(parentId);
    }

    @Override
    public List<Region> selectByIds(List<Integer> idsList) {
        return regionMapper.selectByIds(idsList);
    }

    /**
     * 递归构建树形结构
     * @param allRegions 所有区域列表
     * @param parentId 父级ID
     * @return 树形结构列表
     */
    private List<RegionTreeVO> buildTree(List<Region> allRegions, Integer parentId) {
        List<RegionTreeVO> tree = new ArrayList<>();

        // 找出所有父级ID为指定值的区域
        List<Region> parentRegions = allRegions.stream()
                .filter(region ->
                        (parentId == null && region.getParentId() == null) ||
                                (parentId != null && parentId.equals(region.getParentId()))
                )
                .collect(Collectors.toList());

        // 递归构建子树
        for (Region region : parentRegions) {
            RegionTreeVO node = new RegionTreeVO();
            BeanUtils.copyProperties(region, node);

            // 查找子节点
            List<RegionTreeVO> children = buildTree(allRegions, region.getRegionId());
            if (!CollectionUtils.isEmpty(children)) {
                node.setChildren(children);
            }

            tree.add(node);
        }

        return tree;
    }
}